package com.docwei.thansgank.fragment.fuli;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.docwei.thansgank.R;
import com.docwei.thansgank.adapter.MyFuliAdapter;
import com.docwei.thansgank.callback.ResultBean;
import com.docwei.thansgank.fragment.BaseFragement;
import com.docwei.thansgank.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tobo on 17/2/3.
 */

public class FuliFragment extends BaseFragement implements SwipeRefreshLayout.OnRefreshListener,FuliContract.View{


    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    public List<ResultBean.GankBean> mList = new ArrayList<>();
    private MyFuliAdapter mAdapter;
    private FuliPresenter mFuliPresenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fuli, null);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
        initData();

        return view;

    }

    private void initData() {
        mSwipeRefreshLayout.setProgressViewOffset(false, 0, DensityUtil.dip2px(getActivity(), 24));
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.teal, R.color.orange);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.HORIZONTAL));
        mAdapter = new MyFuliAdapter(mList, getActivity());
        mRecyclerView.setAdapter(mAdapter);
        mFuliPresenter = new FuliPresenter(this);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }


    @Override
    protected void pullData() {
        mFuliPresenter.pullData();
    }

    @Override
    public void onRefresh() {
        mFuliPresenter.pullData();
    }

    @Override
    public void showSwipeRefreshLayout() {
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideSwipeRefreshLayout() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showPullDataSuccess(ResultBean data) {
        List<ResultBean.GankBean> results = data.getResults();
        mList.addAll(results);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showPullDataError(String errMessage) {
        Toast.makeText(getActivity(), errMessage, Toast.LENGTH_SHORT);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //就算这个没有被调用,也不会造成内存泄漏
        mFuliPresenter.detachUiView();
    }
}
